home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / GameKit / Examples / NX_Invaders / AnimTester / GameBrain.m < prev    next >
Encoding:
Text File  |  1995-06-12  |  7.7 KB  |  309 lines

  1. /* Generated by Interface Builder */
  2.  
  3. #import "GameBrain.h"
  4. #import "PreferencesBrain.h"
  5. #import "InfoController.h"
  6. #import "WinDel.h"
  7. #import <stdio.h>
  8. #import <string.h>
  9.  
  10. static int ghostScores[4] = { 200, 400, 800, 1600 };
  11.  
  12.  
  13. @implementation GameBrain
  14.  
  15. - init        // designated initializer sets up game variables
  16. {        // to sensible values.
  17.     score = 0;
  18.     level = 0;
  19.     paused = NO;
  20.     ranOnce = NO;
  21.     
  22.     return self;
  23. }
  24.  
  25. /* methods to get at important variables */
  26. - (int)startLevel { return [preferencesBrain startLevel]; }
  27. - (int)level      { return level;      }
  28. - (int)speed      { return speed;      }
  29. - (int)paused     { return paused;     }
  30.  
  31. - showHigh
  32. {            // update the "high score field    
  33.     [topScoreText setIntValue:[highScoreTable highScore:0]];
  34.     return self;
  35. }
  36.  
  37. - addToScore:(int)increment    // update the player's score
  38. {
  39.     score += increment;
  40.     if (score - lastBonus >= pointsToNextBonus) {
  41.         //[pacsLeft incPacs];
  42.         lastBonus += pointsToNextBonus;
  43.         // assures bonus at 10000, 35000, 55000, 75000, and every 25000 after
  44.         if ((pointsToNextBonus < 20000) || ((pointsToNextBonus == 20000) &&
  45.             (lastBonus >= 75000)))
  46.                 pointsToNextBonus += 5000;
  47.     }
  48.     [scoreText setIntValue:score];
  49.     if ((score > [highScoreTable highScore:0]) && ![gameScreen demoMode]) {
  50.         [topScoreText setIntValue:score];
  51.     }
  52.     return self;
  53. }
  54.  
  55. - zeroScore
  56. {
  57.     score = 0;            // clear score
  58.     [self addToScore:0];    // force display
  59.     return self;
  60. }
  61.  
  62. - gameOver:sender        // end the game, take high scores, etc.
  63. {                // calls gameOver method (this is IB wrapper)
  64.     [self gameOver];
  65.     return self;
  66. }
  67.  
  68. - pauseGame:sender        // toggle pause status of the game
  69. {
  70.     const char *title = [pauseMenuCell title];
  71.     
  72.     if (!strcmp(title,"Pause")) {  // returns zero if equal
  73.         [self pause];
  74.     } else {
  75.         [self unpause];
  76.     } 
  77.     return self;
  78. }
  79.  
  80. - (int)pause                // pause game  
  81. {
  82.     if ([gameScreen gameState]==GAMEOVER) return NO; // no pausing if over
  83.     [pauseMenuCell setTitle:"Unpause"];
  84.     [gameScreen pause:self];
  85.     [gameWindow setTitle:"UNDERminer - Paused"];
  86.     paused = YES;
  87.     return YES;
  88. }
  89.  
  90. - unpause            // unpause game  
  91. {
  92.     if ([gameScreen gameState]==GAMEOVER) {
  93.         [pauseMenuCell setEnabled:NO];
  94.         return self; // no pausing if over
  95.     }
  96.     [pauseMenuCell setTitle:"Pause"];
  97.     [gameScreen unpause:self];
  98.     [gameWindow setTitle:"Gold Mine"];
  99.     paused = NO;
  100.     return self;
  101. }
  102.  
  103. - startNewGame:sender        // starts a new game
  104. {    
  105.     if (([preferencesBrain alert]) && ([gameScreen gameState]!=GAMEOVER)) {
  106.         if ([highScoreTable highScore:9] < score) {
  107.             if (NXRunAlertPanel("You've got a high score!",
  108.                 "Do you want to throw away the current game?",
  109.             "You betcha!", "Um, no.", NULL) != NX_ALERTDEFAULT) {
  110.         return self;  // allow "graceful escape"
  111.         }
  112.     } else {
  113.             if (NXRunAlertPanel(NULL,
  114.                 "Do you want to throw away the current game?",
  115.                 "You betcha!", "Um, no.", NULL) != NX_ALERTDEFAULT) {
  116.             return self;  // allow "graceful escape"
  117.             }
  118.         }
  119.     }
  120.     [topScoreText setIntValue:[highScoreTable highScore:0]];
  121.     score = 0;
  122.     level = 0;
  123.     lastBonus = 0;
  124.     pointsToNextBonus = 10000;    // first bonus at 10000 points
  125.     ranOnce = YES;
  126.     [pauseMenuCell setEnabled:YES];
  127.     [scoreText setIntValue:score];
  128.     [self nextLevel:self];   // by the nextLevel: method.
  129.     if (paused) [self unpause];
  130.     [gameWindow makeKeyAndOrderFront:self];
  131.     //[pacsLeft setNumUp:3];    // start with 3 pacs
  132.     [gameScreen restartGame];
  133.     return self;
  134. }
  135.  
  136. - unpauseGame:sender        // unpause the game 
  137. {
  138.     return [self pauseGame:sender]; // handles toggle of menuCell
  139. }
  140.  
  141. - nextLevel:sender        // move to next level-- called when all viruses
  142.                 // are gone or to start game
  143. {
  144.     level++;
  145.     [levelText setIntValue:level];
  146.     [gameScreen setUpScreen];
  147.     return self;
  148. }
  149.  
  150. - gameOver            // tidy up game, allow high score name entry
  151.                 
  152. {
  153.     // remove demo mode title...
  154.     if ([gameScreen demoMode]) [gameWindow setTitle:"Gold Mine"];
  155.     // if not demo, it's possible to get a high score entry...
  156.     else [highScoreTable putInHighScores:score];
  157.     [pauseMenuCell setEnabled:NO];
  158.     return self;
  159. }
  160.  
  161.  
  162. - (int)ateGhost
  163. {
  164.     int temp = ghostScores[ghostCount];
  165.     
  166.     [self addToScore:temp];
  167.     ghostCount++; ghostCount &= 0x3;
  168.     return temp;
  169. }
  170.  
  171. - resetGhostScore
  172. {
  173.     ghostCount = 0;
  174.     return self;
  175. }
  176.  
  177.  
  178.  
  179. /****
  180. *****  Application DELEGATE methods.  Special things to do on startup, unhide,
  181. *****  hide, and so on.
  182. ****/
  183.  
  184. - appWillInit:sender        // after init, but before 1st event.
  185. {
  186.     alert = NXGetAlertPanel([NXApp appName],
  187.         "Just a moment while I load the images...", NULL, NULL, NULL);
  188.     [alert makeKeyAndOrderFront:self];
  189.     return self;
  190. }
  191.  
  192. - appDidInit:sender        // after init, but before 1st event.
  193. {
  194.     [pauseMenuCell setEnabled:NO];
  195.     
  196.     // methods to load the stuff that takes a while.
  197.     [loadingPanel makeKeyAndOrderFront:self];
  198.     [gameScreen loadPix];            // images, background
  199.     [loadingPanel orderOut:self];
  200.  
  201.     [preferencesBrain readDefaults:self];
  202.     [gameScreen getPreferences];    // set up final init.
  203.     [gameScreen registerWindow];    // set up dragging.
  204.     [[gameScreen animate:self] update];        // start up animation
  205.     [alert orderOut:self];
  206.     NXFreeAlertPanel(alert);
  207.     [topScoreText setIntValue:[highScoreTable highScore:0]];
  208.     [levelText setIntValue:1];
  209.     [scoreText setIntValue:0];
  210.     [[scoreText window] orderFront:self];    // get the stats window visible
  211.     [gameWindow makeKeyAndOrderFront:self]; // activate game window
  212.     [gameWindow makeFirstResponder:gameScreen]; // set up first responder
  213.     if ([preferencesBrain firstTimeCheck]) [infoController readme:self];
  214.     else if ([preferencesBrain autoStart]) [self startNewGame:self];
  215.     return self;
  216. }
  217.  
  218. - appDidBecomeActive:sender
  219. {
  220.     [gameWindow makeKeyAndOrderFront:self];
  221.     [gameWindow makeFirstResponder:gameScreen];
  222.     if ([preferencesBrain autoUnPause]) [self unpause];
  223.  
  224.     // make sure the windows are layered properly
  225.     if ([[[scoreText window] delegate] windowUp])
  226.         [[scoreText window] orderFront:self];
  227.     [gameWindow orderFront:self];
  228.  
  229.     return self;
  230. }
  231.  
  232. - appDidHide:sender
  233. {
  234.     [self pause];
  235.     return self;    // pause game on Command-h
  236. }
  237.  
  238. - appDidResignActive:sender
  239. {
  240.     if ([gameScreen demoMode]) return self;
  241.     [self pause];
  242.     return self;    // pause game on app deactivate
  243. }
  244.  
  245. - appDidUnhide:sender
  246. {
  247.     [gameWindow makeKeyAndOrderFront:self];
  248.     [gameWindow makeFirstResponder:gameScreen];
  249.    if ([preferencesBrain autoUnPause]) [self unpause];
  250.     // make sure the windows are layered properly
  251.     if ([[[scoreText window] delegate] windowUp])
  252.         [[scoreText window] orderFront:self];
  253.     [gameWindow orderFront:self];
  254.     return self;
  255. }
  256.  
  257. - appWillTerminate:sender        // update DEFAULTS here 
  258. {
  259.     [preferencesBrain writeDefaults:self];
  260.     [highScoreTable writeHighScores];
  261.     return self;
  262. }                    
  263.  
  264. - quit:sender
  265. {
  266.     if (([gameScreen gameState] != GAMEOVER) && (![gameScreen demoMode]) &&
  267.             ([preferencesBrain alert])) {
  268.         // Verify that player wants to leave game
  269.             [self pause];
  270.         if (!NXRunAlertPanel(NULL,
  271.                 "There's a game in progress... Do you really want to quit?",
  272.                 "Absolutely.", "No way!", NULL)) {
  273.             return [self unpause];
  274.     }    }
  275.     return [NXApp terminate:sender];
  276. }
  277.  
  278. - windowDidResginMain:sender    // do pause if window loses main status
  279. {
  280.     [self pause];
  281.     return self;    // pause game
  282. }
  283.  
  284. - windowDidResignKey:sender        // do pause if window loses key status
  285. {
  286.     [self pause];
  287.     return self;    // pause game
  288. }
  289.  
  290. - windowDidBecomeKey:sender        // do unpause if window gains key status 
  291. {
  292.     [gameWindow makeFirstResponder:gameScreen];
  293.     if ([preferencesBrain autoUnPause]) [self unpause];    // unpause on unhide*/
  294.     return self;
  295. }
  296.  
  297. - windowDidMove:sender        // move status with game window 
  298. {
  299.     //NXRect gameFrame;
  300.     
  301.     //[gameWindow getFrame:&gameFrame];
  302.     //[[scoreText window] moveTo:(NX_X(&gameFrame) - 137)
  303.     //    :NX_Y(&gameFrame)];
  304.     return self;
  305. }
  306.  
  307.  
  308. @end
  309.